package com.ruoyi.point.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.point.domain.Logistics;
import com.ruoyi.point.service.ILogisticsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物流信息Controller
 * 
 * @author hy
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/point/logistics")
public class LogisticsController extends BaseController
{
    @Autowired
    private ILogisticsService logisticsService;

    /**
     * 查询物流信息列表
     */
    @PreAuthorize("@ss.hasPermi('point:logistics:list')")
    @GetMapping("/list")
    public TableDataInfo list(Logistics logistics)
    {
        startPage();
        List<Logistics> list = logisticsService.selectLogisticsList(logistics);
        return getDataTable(list);
    }

    /**
     * 导出物流信息列表
     */
    @PreAuthorize("@ss.hasPermi('point:logistics:export')")
    @Log(title = "物流信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Logistics logistics)
    {
        List<Logistics> list = logisticsService.selectLogisticsList(logistics);
        ExcelUtil<Logistics> util = new ExcelUtil<Logistics>(Logistics.class);
        util.exportExcel(response, list, "物流信息数据");
    }

    /**
     * 获取物流信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('point:logistics:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(logisticsService.selectLogisticsById(id));
    }

    /**
     * 查询物流信息列表（多表联查，包含订单信息）
     */
    @GetMapping("/list-with-order")
    public TableDataInfo getListWithOrder(Logistics logistics)
    {
        startPage();
        List<Logistics> list = logisticsService.selectLogisticsListWithOrder(logistics);
        return getDataTable(list);
    }

    /**
     * 根据订单ID查询物流信息（多表联查）
     */
    @GetMapping("/by-order/{orderId}")
    public AjaxResult getLogisticsByOrderId(@PathVariable("orderId") String orderId)
    {
        return success(logisticsService.selectLogisticsByOrderId(orderId));
    }

    /**
     * 根据用户ID查询物流信息（多表联查）
     */
    @GetMapping("/by-user/{userId}")
    public AjaxResult getLogisticsByUserId(@PathVariable("userId") Integer userId)
    {
        return success(logisticsService.selectLogisticsByUserId(userId));
    }

    /**
     * 根据地址ID查询物流信息（多表联查）
     */
    @GetMapping("/by-address/{addressId}")
    public AjaxResult getLogisticsByAddressId(@PathVariable("addressId") Integer addressId)
    {
        return success(logisticsService.selectLogisticsByAddressId(addressId));
    }

    /**
     * 查询所有处理中的订单（用于物流管理）
     */
    @GetMapping("/processing-orders")
    public AjaxResult getProcessingOrdersForLogistics()
    {
        return success(logisticsService.selectProcessingOrdersForLogistics());
    }

    /**
     * 新增物流信息并更新订单状态
     */
    @PreAuthorize("@ss.hasPermi('point:logistics:add')")
    @Log(title = "物流信息", businessType = BusinessType.INSERT)
    @PostMapping("/add-and-update-order")
    public AjaxResult addLogisticsAndUpdateOrder(@RequestBody Logistics logistics)
    {
        System.out.println("收到新增物流请求，订单ID: " + logistics.getOrderId());
        System.out.println("快递公司: " + logistics.getExpressCompany());
        System.out.println("快递单号: " + logistics.getExpressNo());
        
        int result = logisticsService.insertLogisticsAndUpdateOrder(logistics);
        
        if (result > 0) {
            System.out.println("物流信息新增成功，订单状态已更新为已发货");
        } else {
            System.out.println("物流信息新增失败");
        }
        
        return toAjax(result);
    }

    /**
     * 更新物流状态为已签收
     */
    @PreAuthorize("@ss.hasPermi('point:logistics:edit')")
    @Log(title = "物流签收", businessType = BusinessType.UPDATE)
    @PutMapping("/received/{logisticsId}")
    public AjaxResult updateToReceived(@PathVariable("logisticsId") Integer logisticsId)
    {
        System.out.println("收到物流签收请求，物流ID: " + logisticsId);
        
        int result = logisticsService.updateLogisticsToReceived(logisticsId);
        
        if (result > 0) {
            System.out.println("物流状态已更新为已签收，物流ID: " + logisticsId);
        } else {
            System.out.println("物流状态更新失败，物流ID: " + logisticsId);
        }
        
        return toAjax(result);
    }

    /**
     * 新增物流信息
     */
    @PreAuthorize("@ss.hasPermi('point:logistics:add')")
    @Log(title = "物流信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Logistics logistics)
    {
        return toAjax(logisticsService.insertLogistics(logistics));
    }

    /**
     * 修改物流信息
     */
    @PreAuthorize("@ss.hasPermi('point:logistics:edit')")
    @Log(title = "物流信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Logistics logistics)
    {
        return toAjax(logisticsService.updateLogistics(logistics));
    }

    /**
     * 删除物流信息
     */
    @PreAuthorize("@ss.hasPermi('point:logistics:remove')")
    @Log(title = "物流信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(logisticsService.deleteLogisticsByIds(ids));
    }
}
